package com.water.wfo.component.util;

import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.Random;

/**
 * packageName com.water.wfo.component.util
 *
 * @author junhuili
 * @version JDK 8
 * @date 2024/5/28
 * @description TODO
 */
public class StringUtil extends StringUtils {
    private static final String NULL_STR = "null";

    /**
     * 判断参数是否为空或全空格
     *
     * @param objects
     * @return
     */
    public static boolean isBlank(final Object... objects) {
        Boolean result = Boolean.FALSE;
        if (null == objects) {
            return result.booleanValue();
        }
        for (Object object : objects) {
            if (object == null || "".equals(object.toString().trim())
                    || NULL_STR.equals(object.toString().trim())
                    || "[null]".equals(object.toString().trim())
                    || "[]".equals(object.toString().trim())) {
                result = Boolean.TRUE;
                break;
            }
        }
        return result.booleanValue();
    }

    /**
     *
     * @Description: 判断多个参数equals 只要符合一个即为True
     * @param list
     * @return
     */
    public static boolean equalsArr(Object... list) {
        boolean isEquals = false;
        if (null == list) {
            return isEquals;
        }
        for (int i = 0; i < list.length; i++) {
            if (i > 0 && list[0] != null && list[0].equals(list[i])) {
                isEquals = true;
                break;
            }
        }
        return isEquals;
    }

    /**
     * 判断参数是否不为空，且不为空格
     *
     * @param objects
     * @return
     */
    public static boolean isNotBlank(final Object... objects) {
        return !isBlank(objects);
    }

    /**
     * 截取返回分割字符串的左侧部分
     *
     * @param str
     *            源字符串
     * @param separator
     *            分割字符串
     * @return
     */
    public static String substringBefore(String str, String separator) {
        if (!isEmpty(str) && separator != null) {
            if (separator.isEmpty()) {
                return "";
            } else {
                int pos = str.indexOf(separator);
                return pos == -1 ? str : str.substring(0, pos);
            }
        } else {
            return str;
        }
    }

    /**
     * 创建Token
     * @return Token
     */
    public static String createToken() {
        Random random = new Random();
        Date date = new Date();
        String token = date.getTime() + "" + random.nextInt(999999999);
        return token;
    }

    /**
     * 返回value，如果value值为"null"，则返回空字符串
     *
     * @param value
     * @return
     */
    public static String getValueNotNull(String value) {
        if (NULL_STR.equals(value)) {
            value = "";
        }
        return value;
    }

    /**
     * 按特定的编码格式获取长度 转换中文 字节数 : 编码：GBK 2,UTF-8 3 编码：UTF-8
     */
    public static String getWordString(String str, String code, int subLength) {
        try {
            if (StringUtil.isEmpty(str)) {
                return null;
            }
            if (StringUtil.isEmpty(code)) {
                if (str.length() > subLength) {
                    str = str.substring(0, subLength);
                }
                return str;
            }
            byte[] b = str.getBytes(code);
            int length = b.length;
            if (length > subLength) {
                int tempSubLength = subLength;
                // 截取字节数
                String subStr = str.substring(0, str.length() < subLength ? str.length() : subLength);// 截取的子串
                // 截取子串的字节长度
                int subStrByetsL = subStr.getBytes(code).length;
                // 说明截取的字符串中包含有汉字
                while (subStrByetsL > tempSubLength) {
                    int subSLengthTemp = --subLength;
                    subStr = str.substring(0, subSLengthTemp > str.length() ? str.length() : subSLengthTemp);
                    subStrByetsL = subStr.getBytes(code).length;
                }
                return subStr;
            }
        } catch (Exception e) {
        }
        return str;
    }

    /**
     * 替换特殊字符[解决页面开单子件单号特殊字符] 不包含逗号
     *
     * @param str
     * @return
     */
    public static String replaceAllStr(String str) {
        if (StringUtil.isEmpty(str)) {
            return null;
        }
        try {
            String regEx = "[`\n\t\r~!@#$%^&*()+=|{}':;'\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。， 、？]";
            str = str.replaceAll(regEx, "");
        } catch (Exception e) {
        }
        return str;
    }

    /**
     * 处理字符串
     *
     * @param strs
     * @return
     */
    public static String formatStr(String strs) {
        if (StringUtils.isEmpty(strs)) {
            return null;
        }
        StringBuffer sb = new StringBuffer();
        String[] arr = strs.split(",");
        for (int i = 0; i < arr.length; i++) {
            if (StringUtils.isNotEmpty(arr[i]) && !StringUtils.equalsIgnoreCase(arr[i], "null")) {
                sb.append(arr[i]);
                sb.append(",");
            }
        }
        String childs = sb.toString();
        if (StringUtils.isNotEmpty(childs)) {
            childs = (childs).substring(0, childs.length() - 1);
        }
        return childs;
    }

    /**
     * 替换指定位置的字符
     *
     * @param oldValue	可以分割的字符串
     * @param newValue	替换字符
     * @param index
     */
    public static String replaceCharArrayAtIndex(String oldValue, String newValue, int index, String split){

        if(isEmpty(oldValue) || isEmpty(newValue)){
            return oldValue;
        }

        StringBuilder simpleNameBuilder = new StringBuilder();
        String[] oldSimpleNameArray = oldValue.split(split);

        // 如果oldValue在index位置为空，则直接将newValue添加到该位置返回
        if(index == oldSimpleNameArray.length){
            for(String str : oldSimpleNameArray){
                simpleNameBuilder.append(str).append(split);
            }
            simpleNameBuilder.append(newValue);
            return simpleNameBuilder.toString();
        }

        if(index > oldSimpleNameArray.length-1){
            return oldValue;
        }

        for(int i=0; i< oldSimpleNameArray.length; i++){
            if(i==index){
                simpleNameBuilder.append(newValue);
            }else{
                simpleNameBuilder.append(oldSimpleNameArray[i]);
            }
            if(i<(oldSimpleNameArray.length-1)){
                simpleNameBuilder.append(split);
            }
        }
        return simpleNameBuilder.toString();
    }

    /**
     * 替换相同位置的字符
     *
     * @param oldValue	可以分割的字符串
     * @param newValue	可以分割的字符串
     * @param index
     */
    public static String replaceCharArrayAtCommonIndex(String oldValue, String newValue, int index, String split){

        if(isEmpty(oldValue) || isEmpty(newValue)){
            return oldValue;
        }

        StringBuilder simpleNameBuilder = new StringBuilder();
        String[] oldSimpleNameArray = oldValue.split(split);
        String[] newSimpleNameArray = newValue.split(split);

        if(index > oldSimpleNameArray.length-1 || index > newSimpleNameArray.length-1){
            return oldValue;
        }

        for(int i=0; i< oldSimpleNameArray.length; i++){
            if(i==index && newSimpleNameArray.length>index){
                simpleNameBuilder.append(newSimpleNameArray[i]);
            }else{
                simpleNameBuilder.append(oldSimpleNameArray[i]);
            }
            if(i<(oldSimpleNameArray.length-1)){
                simpleNameBuilder.append(split);
            }
        }
        return simpleNameBuilder.toString();
    }

    /**
     *
     * Created on 2014-12-11
     * <p>
     * Description:[为 null 的字符设置为 ""]
     * </p>
     *
     * @author:王孟
     * @update:[日期YYYY-MM-DD] [更改人姓名]
     * @param str
     *            字符串
     * @return String .
     */
    public static String defaultIfNull(String str) {
        return (str == null) ? "" : str;
    }
}
